package edu.wayne.cs.csc4110.hw6.framework.plugin;

import java.awt.Cursor;
import java.awt.Graphics;

import edu.wayne.cs.csc4110.hw6.framework.Util.Constants;
import edu.wayne.cs.csc4110.hw6.interfaces.plugin.Plugin;
import edu.wayne.cs.csc4110.hw6.interfaces.user.User;
import edu.wayne.cs.csc4110.hw6.interfaces.user.UserFactory;

/**
 * This Plugin will draw the game frame by call to the constructor
 */

public abstract class ActivePlugin implements Plugin {

	private String title;
	private int width = Constants.PLUGIN_WIDTH,
			height = Constants.PLUGIN_HEIGHT;
	private UserFactory userFactory;
	private String identifer;
	private User activeUser;

	public ActivePlugin(String title, UserFactory userFactory) {
		if(title.length()< 3){
			throw new IllegalArgumentException("The Title of your game cannot be less than 3 letters");
		}
		this.userFactory = userFactory;
		this.title = title;
		this.identifer = title + PluginUID.getUID();
	}

	/**
	 * After each plugin loop the framework will call this method to update any necessary game objects
	 */
	public abstract void update();

	/**
	 * Draw the plugin objects to the screen
	 * 
	 * @param gfx
	 */
	public abstract void draw(Graphics gfx);

	/**
	 * To get a new instance of a game at runtime to allow multiple plugin
	 * instances to be created when a user gets logged in.
	 * 
	 * @param u
	 *            , the user to register to the plugin
	 * @return the active plugin instance
	 */
	public abstract ActivePlugin newInstance(User u);

	@Override
	public String getUserExtension() {
		
		return "."+title.substring(0, 3);
	}

	/**
	 *  Override this method to allow custom cursors for your plugin
	 * @return a custom cursor for the game
	 */
	public Cursor getCursor() {
		return Cursor.getDefaultCursor();
	}

	/**
	 * Restarts the game where game objects are reinitialized for a game
	 * restart.
	 */
	public abstract void restart();

	public User createUser(String username) {
		activeUser = getUserFactory().createUser(username);
		return activeUser;
	}

	public User createUser(User user) {
		User temp = getUserFactory().createUser("");
		user = temp.getClass().cast(user);
		activeUser = user;
		return user;

	}

	public void setUser(User user) {
		this.activeUser = user;
	}

	public User getUser() {
		return activeUser;
	}

	@Override
	public String getGameTitle() {
		return title;
	}

	@Override
	public int getWidth() {
		return width;
	}

	@Override
	public int getHeight() {
		return height;
	}

	@Override
	public UserFactory getUserFactory() {
		return userFactory;
	}

	@Override
	public int hashCode() {
		return identifer.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof ActivePlugin))
			return false;

		return this.identifer.equals(((ActivePlugin) obj).identifer);
	}

}
